package com.shigu.itemCate.action;

import com.shigu.MallResponse;
import com.shigu.PageResponse;
import com.shigu.exceptions.WapException;

import com.shigu.itemCate.api.ItemCateService;
import com.shigu.itemCate.bo.AddCateRequest;
import com.shigu.itemCate.bo.CateBo;
import com.shigu.itemCate.bo.ModCateBo;
import com.shigu.itemCate.bo.ModCateRequest;
import com.shigu.itemCate.vo.CateVo;
import org.apache.commons.lang3.StringUtils;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/*
 * create by  过儿  2019-06-05
 *
 */
@Controller
@RequestMapping("adminjson")
public class AdminCateController {
    @Resource(name = "itemCateServiceImpl")
    ItemCateService itemCateService;

    @RequestMapping("selMainCate")
    @ResponseBody
    public PageResponse<CateVo> selMainCate(){
        return itemCateService.selCate("-1");
    }


    @RequestMapping("addCate")
    @ResponseBody
    public MallResponse addCate(AddCateRequest request) throws WapException {
        CateBo bo = new CateBo();
        bo.setCateName(request.getCateName());
        bo.setParentCid(request.getParentCid());
        if(StringUtils.isNotBlank(request.getImgUrl())){
            bo.setImgUrl(request.getImgUrl());
        }
        bo.setSortOrder(request.getWeight());
        itemCateService.addCate(bo);
        return new MallResponse();
    }

    @RequestMapping("deleteCate")
    @ResponseBody
    public MallResponse deleteCate(String cid) throws WapException {
        itemCateService.delCate(Long.valueOf(cid));
        return new MallResponse();
    }

    @RequestMapping("modifyCate")
    @ResponseBody
    public MallResponse modifyCate(ModCateRequest request){
        ModCateBo bo = new ModCateBo();
        if(StringUtils.isNotBlank(request.getImgUrl())){
            bo.setImgUrl(request.getImgUrl());
        }
        bo.setCid(Long.valueOf(request.getCid()));
        bo.setSortOrder(request.getWeight());
        bo.setCateName(request.getCateName());

        itemCateService.modifyCate(bo);
        return new MallResponse();
    }
}
